package com.afdor.rws.core.strategy.impl;

import com.afdor.rws.annotation.extension.Extension;
import com.afdor.rws.core.strategy.TimestampStrategy;
import org.apache.commons.lang3.time.FastDateFormat;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 本地时间获取
 *
 * @author 悭梵
 * @date Created in 2018-08-03 19:23
 */
@Extension(value = "local_timestamp", order = Extension.NORMAL_ORDER - 100, interfaceClass = TimestampStrategy.class)
public class LocalTimestampStrategyImpl implements TimestampStrategy {

    private static final Map<String, FastDateFormat> DATE_FORMAT_MAP = new ConcurrentHashMap<>(16);

    @Override
    public long get() {
        return System.currentTimeMillis();
    }

    /**
     * 采用 common-lang3的FastDateFormat实现
     *
     * @param format 格式化字符串
     * @return
     */
    @Override
    public String get(String format) {
        FastDateFormat dateFormat = DATE_FORMAT_MAP.get(format);
        if (dateFormat == null) {
            DATE_FORMAT_MAP.put(format, FastDateFormat.getInstance(format));
            dateFormat = DATE_FORMAT_MAP.get(format);
        }
        return dateFormat.format(get());
    }
}
